package com.xingda.packing.dmss.minitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingda.packing.dmss.minitor.entity.PkConsumerDemand;
import com.xingda.packing.dmss.minitor.mapper.PkConsumerDemandMapper;
import com.xingda.packing.dmss.minitor.service.IPkConsumerDemandService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PkConsumerDemandImpl extends ServiceImpl<PkConsumerDemandMapper, PkConsumerDemand>
        implements IPkConsumerDemandService {

    @Resource
    PkConsumerDemandMapper pkConsumerDemandMapper;

    @Override
    public PkConsumerDemand getByConsumerName(String consumerName) {
        LambdaQueryWrapper<PkConsumerDemand> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PkConsumerDemand::getConsumerName, consumerName);

        List<PkConsumerDemand> list = pkConsumerDemandMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public void saveorUpdateDemandBy(String consumerName, String defaultDemand) {
        LambdaQueryWrapper<PkConsumerDemand> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PkConsumerDemand::getConsumerName, consumerName);
        List<PkConsumerDemand> list = pkConsumerDemandMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            PkConsumerDemand pkConsumerDemand = new PkConsumerDemand();
            pkConsumerDemand.setConsumerName(consumerName);
            pkConsumerDemand.setDefaultDemand(defaultDemand);
            pkConsumerDemandMapper.insert(pkConsumerDemand);
        } else {
            PkConsumerDemand pkConsumerDemand = list.get(0);
            pkConsumerDemand.setDefaultDemand(defaultDemand);
            pkConsumerDemandMapper.updateById(pkConsumerDemand);
        }
    }

}
